<?php
session_start();
require_once("/../controllers/GenerateMonthlyBillsController.php");
class Unit{
	var $password;
	var $name;
	var $homeaddress;
	var	$studnum;
	var	$cellnum;
	var	$course;
	var	$college;
	var	$classification;
	var	$parents;
	var	$contact;
	var	$occupants;
	var	$deposit;
	var	$rent;
	var	$start;
	var	$debts;
	
	var $details;
	function isOccupied($unit){
		$result = mysql_query("SELECT isoccupied FROM unit where unit_name = '$unit'")
		or die(mysql_error());  
		$row = mysql_fetch_array( $result );
		if($row['isoccupied']==1)
		return true;
		else return false;
		
	}
	function connect(){
		$user="root";
		$password="";
		$database="whitehouse";
		mysql_connect(localhost,$user,$password);
		@mysql_select_db($database) or die( "Unable to select database");
		
		
		
		$query1 = "SELECT * from bill_month WHERE month1 = month(curdate()) and year1 = year(curdate())";
		//echo $query1;
		$result1 = mysql_query($query1)
		or die(mysql_error());
		$num = mysql_num_rows($result1);
		//echo $num;
		if($num==0){
			$generate = new GenerateMonthlyBillsController();
			$query = "INSERT into bill_month(month1,year1) values (month(curdate()),year(curdate()))";
			mysql_query($query);
		}
	}
	function startContract($unitname){
		$name = $this->details['name'];
		$pass = $this->details['pass'];
		$homeaddress = $this->details['homeaddress'];
		$studnum = $this->details['studnum'];
		$cellnum = $this->details['cellnum'];
		$course = $this->details['course'];
		$college = $this->details['college'];
		$classification = $this->details['classification'];
		$parents = $this->details['parents'];
		$contact = $this->details['contact'];
		$occupants = $this->details['occupants'];
		$deposit = $this->details['deposit'];
		$rent = $this->details['rent'];
		$start = $this->details['start'];
		$debts = $this->details['debts'];
		//echo $unitname;
		$update = "UPDATE unit SET isoccupied = 1, 
		password = '$pass',
		lesse_name = '$name',
		home_address = '$homeaddress',
		stud_num= '$studnum',
		cell_num = $cellnum,
		course = '$course',
		college = '$college',
		classification = '$classification',
		parent_names = '$parents',
		parents_contact_num = '$contact',
		occupants = '$occupants',
		deposit = $deposit,
		monthly_rent = $rent,
		start_date = '$start',
		previous_debts = $debts
		WHERE unit_name = '$unitname'";
		//echo $update;
		mysql_query($update);
	}
	function stopContract($unit_name){
		$update = "UPDATE unit SET isoccupied = 0
		WHERE unit_name = '$unit_name'";
	//	echo $update;
		$flag = mysql_query($update);
		
		return $flag;
		
		
	
	}
	function sendContractDetails($details){
		$this->details['name']=$details['name'];
		$this->details['pass']=$details['pass'];
		$this->details['homeaddress']=$details['homeaddress'];
		$this->details['studnum']=$details['studnum'];
		$this->details['cellnum']=$details['cellnum'];
		$this->details['course']=$details['course'];
		$this->details['college']=$details['college'];
		$this->details['classification']=$details['classification'];
		$this->details['parents']=$details['parents'];
		$this->details['contact']=$details['contact'];
		$this->details['occupants']=$details['occupants'];
		$this->details['deposit']=$details['deposit'];
		$this->details['rent']=$details['rent'];
		$this->details['start']=$details['start'];
		$this->details['debts']=$details['debts'];
	}
	
	function isValidUnitLogin($loginDetails){
		$unit = $loginDetails['username'];
		$result = mysql_query("SELECT password FROM unit where unit_name = '$unit'")
		or die(mysql_error());  
		$row = mysql_fetch_array( $result );
		if($row['password']==$loginDetails['password']){
			return true;
		}
		else return false;

	}
	
	function getAccountInformation($unit){
		$result = mysql_query("SELECT * FROM unit where unit_name = '$unit'")
		or die(mysql_error());  
		$row = mysql_fetch_array( $result );
		$details['name'] = $row['lesse_name'];
		$details['home'] = $row['home_address'];
		$details['studnum'] = $row['stud_num'];
		$details['cellnum'] = $row['cell_num'];
		$details['course'] = $row['course'];
		$details['college'] = $row['college'];
		$details['classification'] = $row['classification'];
		$details['parent'] = $row['parent_names'];
		$details['contact'] = $row['parents_contact_num'];
		$details['occupants'] = $row['occupants'];
		
		return $details;
	}
	
	function getFinancialInformation($unit){
		$result = mysql_query("SELECT * FROM unit where unit_name = '$unit'")
		or die(mysql_error());
		$row = mysql_fetch_array( $result );
		$details['deposit'] = $row['deposit'];
		$details['previous_debts'] = $row['previous_debts'];
		return $details;
	}
	
	function changeAccountInfo($details){
		$unitname = $details['unitname'];
		$name = $details['name'];
		$home = $details['home'];
		$studnum = $details['studnum'];
		$cellnum = $details['cellnum'];
		$course = $details['course'];
		$college = $details['college'];
		$classification = $details['classification'];
		$parents = $details['parents'];
		$contact = $details['contact'];
		$occupants = $details['occupants'];
		$update = "UPDATE unit SET
		lesse_name = '$name',
		home_address = '$home',
		stud_num= '$studnum',
		cell_num = $cellnum,
		course = '$course',
		college = '$college',
		classification = '$classification',
		parent_names = '$parents',
		parents_contact_num = '$contact',
		occupants = '$occupants'
		WHERE unit_name = '$unitname'";
		mysql_query($update);
	}
	
	function updateFinanceInfo($details){
		$deposit = $details['deposit'];
		$debts = $details['debts'];
		$unit_name = $details['unit_name'];
		$update = "UPDATE unit SET
		deposit = $deposit, previous_debts = $debts
		WHERE unit_name = '$unit_name'";
		//echo $update;
		mysql_query($update);
	}
	
	function setPassword($details){
		$unit_name = $details['unit_name'];
		$new = $details['newpass'];
		$update = "UPDATE unit SET password = '$new' where unit_name='$unit_name'";
		mysql_query($update);
	}
	function getPassword(){
		return $this->details['pass'];
	}
	function getName(){
		return $this->details['name'];
	}
	function getHomeAddress(){
		return $this->details['homeaddress'];
	}
	function getStudentNumber(){
		return $this->details['studnum'];
	}
	function getCellphoneNumber(){
		return $this->details['cellnum'];
	}
	function getCourse(){
		return $this->details['course'];
	}
	function getCollege(){
		return $this->details['college'];
	}
	function getClassification(){
		return $this->details['classification'];
	}
	
	function getParents(){
		return $this->details['parents'];
	}
	function getContact(){
		return $this->details['contact'];
	}
	function getOccupants(){
		return $this->details['occupants'];
	}
	function getDeposit(){
		return $this->details['deposit'];
	}
	function getRent(){
		return $this->details['rent'];
	}
	function getStart(){
		return $this->details['start'];
	}
	function getDebts(){
		return $this->details['debts'];
	}
	
	

}
?>